#include "Background.h"
#include "Game.h"

using namespace cocos2d;

Background::~Background()
{
    CCLog("Background destroy");
	// cpp don't need to call super dealloc
	// virtual destructor will do this
}

Background::Background()
:currentCloudTag(0)
{
    CCLog("Background create");
}


// on "init" you need to initialize your instance
bool Background::init()
{
	bool bRet = false;
	do 
	{
	    CCLog("Background init");
		//////////////////////////////////////////////////////////////////////////
		// super init first
		//////////////////////////////////////////////////////////////////////////

		CC_BREAK_IF(! CCLayer::init());


		CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("sprites.png",10);
		this->addChild(batchNode, -1, kSpriteManager);

		CCSprite *background = CCSprite::spriteWithTexture(batchNode->getTexture(), CCRectMake(0,0,320,480));

		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
			
		background->setPosition( ccp(origin.x + background->getContentSize().width/2,
                                 origin.y + visibleSize.height/2) );
		batchNode->addChild(background);

		this->initClouds();
		
		this->schedule( schedule_selector(Background::step));

		bRet = true;
	} while (0);

	return bRet;
}

void Background::menuCloseCallback(CCObject* pSender)
{
	// "close" menu item clicked
	CCDirector::sharedDirector()->end();
}

void Background::initClouds()
{
    CCLog("Background initClouds");
	currentCloudTag = kCloudsStartTag;
	while(currentCloudTag < kCloudsStartTag + kNumClouds) 
	{
		this->initCloud();
		currentCloudTag ++;
	}
}

void Background::initCloud()
{
    CCLog("Background initCloud");
	CCRect rect;
	switch(rand()%3) 
	{
		case 0: rect = CCRectMake(336,16,256,108); break;
		case 1: rect = CCRectMake(336,128,257,110); break;
		case 2: rect = CCRectMake(336,240,252,119); break;
	}

	CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)(this->getChildByTag(kSpriteManager));
	CCSprite *cloud = CCSprite::spriteWithTexture(batchNode->getTexture(), rect);
	batchNode->addChild(cloud, 3, currentCloudTag);
	cloud->setOpacity(128);
}

void Background::resetClouds()
{
    CCLog("Background resetClouds");
	currentCloudTag = kCloudsStartTag;
	CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
	CCLog("Background resetClouds winSize:%f x %f", winSize.width, winSize.height);
	while(currentCloudTag < kCloudsStartTag + kNumClouds) 
	{
		this->resetCloud();

		CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)(this->getChildByTag(kSpriteManager));
		CCSprite *cloud = (CCSprite*)(batchNode->getChildByTag(currentCloudTag));
		CCPoint pos = cloud->getPosition();
		
		pos.y -= winSize.height;
		cloud->setPosition(pos);
		
		currentCloudTag++;
	}
}

void Background::resetCloud()
{
	CCLog("Background resetCloud");
	CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)(this->getChildByTag(kSpriteManager));
	CCSprite *cloud = (CCSprite*)(batchNode->getChildByTag(currentCloudTag));
	
	float distance = rand()%20 + 5;
	
	float scale = 5.0f / distance;
	cloud->setScaleX(scale);
	cloud->setScaleY(scale);
	if(rand()%2==1) cloud->setScaleX(-scale);
	
	CCSize size = cloud->getContentSize();
	CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
	float scaled_width = size.width * scale;
	float x = rand()%((int)winSize.width+(int)scaled_width) - scaled_width/2;
	float y = rand()%((int)winSize.height-(int)scaled_width) + scaled_width/2 + (int)winSize.height;

	CCLog("resetCloud++++++++after  x:%f, y:%f, currentCloudTag:%d", x, y, currentCloudTag);
	cloud->setPosition( ccp(x,y));
}

void Background::step(float dt)
{
	CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)(this->getChildByTag(kSpriteManager));

	CCLog("Background step ");
	int t = kCloudsStartTag;
	for(t; t < kCloudsStartTag + kNumClouds; t++) {
		CCSprite *cloud = (CCSprite*)(batchNode->getChildByTag(t));
		CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
		CCPoint pos = cloud->getPosition();
		CCSize size = cloud->getContentSize();
		pos.x += 0.1f * cloud->getScaleY();
		if(pos.x > winSize.width + size.width/2) {
			pos.x = -size.width/2;
		}
		cloud->setPosition(pos);
	}
}

